using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;

namespace HW2and3
{
    abstract class ModelBase
    {
        private ArrayList observers = new ArrayList();
        protected Thread modelThread;
        protected int size;
        protected int x;
        protected int y;
        volatile protected int sleepInterval;
        volatile protected bool isSuspended;
        volatile protected bool isStopped;
        volatile protected bool isRunning;

        virtual public void Initialize(int sleepInterval, int size, int x, int y)
        {
            this.size = size;
            this.sleepInterval = sleepInterval;
            this.isSuspended = false;
            this.isStopped = false;
            this.isRunning = false;
            this.x = x;
            this.y = y;
        }

        virtual public void Start()
        {
            isRunning = true;
            modelThread.Start();
        }

        virtual public void Pause()
        {
            if (isRunning)
            {
                if (isSuspended == false)
                    modelThread.Suspend();
                else
                    modelThread.Resume();
                isSuspended = !isSuspended;
            }
        }

        virtual public void Destroy()
        {
            this.isRunning = false;
            this.isStopped = true;
        }

        virtual public void Sleep()
        {
            if (isRunning)
                Thread.Sleep(this.sleepInterval);
        }

        public bool IsRunning
        {
            get { return isRunning; }
        }

        public bool IsStopped
        {
            get { return isStopped; }
        }

        public int SleepInterval
        {
            get { return sleepInterval; }
            set { sleepInterval = value; }
        }

        public int Size
        {
            get { return size; }
        }

        public int X
        {
            get { return x; }
        }

        public int Y
        {
            get { return y; }
        }

        public void NotifyAll(StateInterface state, string msg, string info)
        {
            foreach (Observer observer in observers)
            {
                observer.Update(state, msg, info);
            }
        }

        public void RegisterObserver(Observer o)
        {
            observers.Add(o);
        }

        public void RemoveObserver(Observer o)
        {
            observers.Remove(o);
        }
    }
}
